package com.hl233.system.controller;

import com.alibaba.nacos.shaded.com.google.common.base.Supplier;
import com.hl233.common.constant.Constants;
import com.hl233.common.utils.SecurityUtils;
import com.hl233.system.entity.BackEntity;
import com.hl233.system.mapper.BackMapper;
import com.hl233.system.service.BackService;
import com.hl233.system.util.DateUtil;
import com.hl233.system.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 张振强15065206763
 * @create 2024-09-10
 */

@RestController
@RequestMapping("/system")
public class BackController {

    @Autowired
    private BackService backService;

    @ApiOperation(value = "表查询全部")
    @GetMapping("/queryAll")
    public List<Map> queryAll(@RequestHeader(value = "truth",required = false) String truth, HttpServletRequest request) {
        System.out.println("拿到truth"+truth);
        System.out.println("拿到userid"+request.getHeader(Constants.DETAILS_USER_ID));
        Long userId = SecurityUtils.getUserId();

        System.out.println("通过这个security获取userid:"+userId);
        return backService.queryAll();
    }

    @ApiOperation(value = "新增数据")
    @PostMapping("/add")
    public String add(@RequestBody Map map) {
        System.out.println("能拿到猫"+map);

        BackEntity entity = new BackEntity();
        entity.setName(map.get("ces 09030801")+"");
        entity.setCreatetime(DateUtil.now());
        boolean b =  backService.save(entity);

        System.out.println("处理结果"+b);
        return "1111";
    }

    @GetMapping("/getById1")
    public String getById1(@RequestParam("id") Integer id){
        Long userId = SecurityUtils.getUserId();
        System.out.println("通过这个security获取userid:"+userId);
        BackEntity entity = backService.getById(id);
        System.out.println("拿到的id是什么"+id);
        return entity.toString();
    }

    @GetMapping("/getById2/{id}")
    public String getById2(@PathVariable("id") Integer id){
        Long userId = SecurityUtils.getUserId();

        System.out.println("通过这个security获取userid:"+userId);

        BackEntity entity = backService.getById(id);
        System.out.println("拿到的id是什么"+id);
        return entity.toString();
    }

}
